package com.icss.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.beans.DynamicNewsVO;
import com.icss.beans.HotNewsVO;
import com.icss.beans.NewsTypeVO;
import com.icss.daos.DynamicNewsDao;

public class DynamicNewsServlet extends HttpServlet {

	
	public DynamicNewsServlet() {
		super();
	}

	
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		DynamicNewsDao dndao = new DynamicNewsDao();
		if("show".equals(action)){
			request.getRequestDispatcher("/page/index/dynamicNews/showNews.jsp").forward(request, response);
		}else if("showNews".equals(action)){
			List<DynamicNewsVO> list = dndao.getAllNews();
			request.setAttribute("newsList", list);
			request.getRequestDispatcher("/page/index/dynamicNews/manageNews.jsp").forward(request, response);
		}else if("addNews".equals(action)){
		
			request.getRequestDispatcher("/page/index/dynamicNews/addNews.jsp").forward(request, response);
		}else if("add".equals(action)){
			String title=request.getParameter("newsTitle");
			String context = request.getParameter("newsContext");
			dndao.addNews(title,context);
			request.getRequestDispatcher("DynamicNewsServlet?action=showNews").forward(request, response);			
			
		}else if("edit".equals(action)){
			int newsID = Integer.parseInt(request.getParameter("newsID"));
			DynamicNewsVO vo =dndao.getOneNews(newsID);
			request.setAttribute("DynamicNewsVO", vo);
			request.getRequestDispatcher("/page/index/dynamicNews/editNews.jsp").forward(request, response);
		}else if("update".equals(action)){
			int newsID = Integer.parseInt(request.getParameter("newsID"));
			String title = request.getParameter("newsTitle");
			String newsContext = request.getParameter("newsContext");
			dndao.updateNews(newsID,title,newsContext);
			request.getRequestDispatcher("DynamicNewsServlet?action=showNews").forward(request, response);
			
		}else if("del".equals(action)){
			int newsID = Integer.parseInt(request.getParameter("newsID"));
			dndao.deleteNews(newsID);
			request.getRequestDispatcher("DynamicNewsServlet?action=showNews").forward(request, response);
		}else if("delAllSelect".equals(action)){
			String str = request.getParameter("uidList");
			dndao.delAllSelect(str);
			request.getRequestDispatcher("DynamicNewsServlet?action=showNews").forward(request, response);
		}else if("display".equals(action)){
			int newsID = Integer.parseInt(request.getParameter("newsID"));
			DynamicNewsVO vo =dndao.getOneNews(newsID);
			request.setAttribute("newsvo", vo);
			request.getRequestDispatcher("/page/index/dynamicNews/displayNews.jsp").forward(request, response);
		}else if("displayNews".equals(action)){
			int newsID = Integer.parseInt(request.getParameter("newsID"));
			DynamicNewsVO vo =dndao.getOneNews(newsID);
			request.setAttribute("newsvo", vo);
			
			List<DynamicNewsVO> otherNews = dndao.getotherNews(newsID);
			if(otherNews.size()>0){
				request.setAttribute("isHave", "y");
				request.setAttribute("otherNews", otherNews);
			}else{
				request.setAttribute("isHave", "n");
			}
			request.getRequestDispatcher("/page/index/dynamicNews/indexDisplay.jsp").forward(request, response);
		}else if("displayjj".equals(action)){
			int newsID = Integer.parseInt(request.getParameter("newsID"));
			DynamicNewsVO vo =dndao.getOneNews(newsID);
			request.setAttribute("newsvo", vo);
			request.getRequestDispatcher("/page/index/dynamicNews/displayNewsjj.jsp").forward(request, response);
		}
		dndao.close();
		
	}

	
	public void init() throws ServletException {
		// Put your code here
	}

}
